package com.byb.server.service.impl;

import com.byb.server.repository.WcInsuranceTypeRepository;
import com.byb.server.service.WcInsuranceCategoryService;
import com.byb.server.domain.WcInsuranceCategory;
import com.byb.server.repository.WcInsuranceCategoryRepository;
import com.byb.server.repository.search.WcInsuranceCategorySearchRepository;
import com.byb.server.service.dto.Wc.WcITDTO;
import com.byb.server.service.dto.WcInsuranceCategoryDTO;
import com.byb.server.service.mapper.Wc.WcITMapper;
import com.byb.server.service.mapper.WcInsuranceCategoryMapper;
import com.byb.server.service.util.WcUtil.WcInsuranceTypeGroup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;
import java.util.stream.Collectors;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing WcInsuranceCategory.
 */
@Service
@Transactional
public class WcInsuranceCategoryServiceImpl implements WcInsuranceCategoryService{

    private final Logger log = LoggerFactory.getLogger(WcInsuranceCategoryServiceImpl.class);

    private final WcInsuranceCategoryRepository wcInsuranceCategoryRepository;

    private final WcInsuranceCategoryMapper wcInsuranceCategoryMapper;

    private final WcInsuranceCategorySearchRepository wcInsuranceCategorySearchRepository;

    private final WcInsuranceTypeRepository wcInsuranceTypeRepository;

    private final WcITMapper wcITMapper;
    public WcInsuranceCategoryServiceImpl(WcInsuranceCategoryRepository wcInsuranceCategoryRepository, WcInsuranceCategoryMapper wcInsuranceCategoryMapper, WcInsuranceCategorySearchRepository wcInsuranceCategorySearchRepository, WcInsuranceTypeRepository wcInsuranceTypeRepository, WcITMapper wcITMapper) {
        this.wcInsuranceCategoryRepository = wcInsuranceCategoryRepository;
        this.wcInsuranceCategoryMapper = wcInsuranceCategoryMapper;
        this.wcInsuranceCategorySearchRepository = wcInsuranceCategorySearchRepository;
        this.wcInsuranceTypeRepository = wcInsuranceTypeRepository;
        this.wcITMapper = wcITMapper;
    }

    /**
     * Save a wcInsuranceCategory.
     *
     * @param wcInsuranceCategoryDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public WcInsuranceCategoryDTO save(WcInsuranceCategoryDTO wcInsuranceCategoryDTO) {
        log.debug("Request to save WcInsuranceCategory : {}", wcInsuranceCategoryDTO);
        WcInsuranceCategory wcInsuranceCategory = wcInsuranceCategoryMapper.toEntity(wcInsuranceCategoryDTO);
        wcInsuranceCategory = wcInsuranceCategoryRepository.save(wcInsuranceCategory);
        WcInsuranceCategoryDTO result = wcInsuranceCategoryMapper.toDto(wcInsuranceCategory);
        //wcInsuranceCategorySearchRepository.save(wcInsuranceCategory);
        return result;
    }

    /**
     *  Get all the wcInsuranceCategories.
     *
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<WcInsuranceCategoryDTO> findAll(Pageable pageable) {
        log.debug("Request to get all WcInsuranceCategories");
        return wcInsuranceCategoryRepository.findAll(pageable)
            .map(wcInsuranceCategoryMapper::toDto);
    }

    /**
     *  Get one wcInsuranceCategory by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public WcInsuranceCategoryDTO findOne(Long id) {
        log.debug("Request to get WcInsuranceCategory : {}", id);
        WcInsuranceCategory wcInsuranceCategory = wcInsuranceCategoryRepository.findOne(id);
        return wcInsuranceCategoryMapper.toDto(wcInsuranceCategory);
    }

    /**
     *  Delete the  wcInsuranceCategory by id.
     *
     *  @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete WcInsuranceCategory : {}", id);
        wcInsuranceCategoryRepository.delete(id);
        //wcInsuranceCategorySearchRepository.delete(id);
    }

    /**
     * Search for the wcInsuranceCategory corresponding to the query.
     *
     *  @param query the query of the search
     *  @param pageable the pagination information
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public Page<WcInsuranceCategoryDTO> search(String query, Pageable pageable) {
        log.debug("Request to search for a page of WcInsuranceCategories for query {}", query);
        Page<WcInsuranceCategory> result = wcInsuranceCategorySearchRepository.search(queryStringQuery(query), pageable);
        return result.map(wcInsuranceCategoryMapper::toDto);
    }

    /**
     *  Get all the wcInsuranceCategories for Wc.
     *  @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public List<WcITDTO> findAllForWc() {
        log.debug("Request to get all WcInsuranceCategories for Wc");
        List<WcInsuranceCategoryDTO> list = wcInsuranceCategoryRepository.findAll().stream().map(wcInsuranceCategoryMapper::toDto).collect(Collectors.toList());
        List<WcITDTO> result = wcInsuranceTypeRepository.findAll().stream().map(wcITMapper::toDto).collect(Collectors.toList());
        return  WcInsuranceTypeGroup.GroupData(result,list);
    }
}
